package cn.yunhe.service.impl;

import cn.yunhe.dao.DepartmentDao;
import cn.yunhe.entity.Department;
import cn.yunhe.service.DepartmentService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service("departmentService")
public class DepartmentServiceImpl implements DepartmentService {

    @Resource
    private DepartmentDao departmentDao;


    @Override
    public Page getDepartmentListByPage(Map<String, Object> map) {
        Page page = new Page();

        //获取总条数
        page.setTotal(departmentDao.getDepartmentListCountByPage(map));
        // System.out.println(knowledgePointDao.getKnowledgePointListCountByPage(map));
        // 获取每页大小
        int pageSize = Integer.parseInt(String.valueOf(map.get("pageSize")));
        page.setPageSize(pageSize);

        // 获取当前页码
        int curPageNum = Integer.parseInt(String.valueOf(map.get("pageNum")));
//		page.setPageNum(curPageNum);
        // 计算出总页数
        page.setPageNum((int) (page.getTotal() / pageSize + (page.getTotal() % pageSize == 0 ? 0 : 1)));
        // 启用分页
        page = PageHelper.startPage(curPageNum, pageSize);
        // 查询列表
        List list = departmentDao.getDepartmentListByPage(map);
        return page;
    }

    @Override
    public int addDepartment(Department department) {
        if (department.getDepName()==null){
            return 0;
        }else {
            return departmentDao.addDepartment(department);
        }
    }

    @Override
    public int delDepartment(Department department) {
        return departmentDao.delDepartment(department);
    }

    @Override
    public int updDepartment(Department department) {
        if (department.getDepName()==null){
            return 0;
        }else {
            return departmentDao.updDepartment(department);
        }
    }

    @Override
    public Department getDepartmentById(Department department) {
        return departmentDao.getDepartmentById(department);
    }


    @Override
    public List<Department> getAllDep() {
        return departmentDao.getAllDep();
    }

    @Override
    public Department getDepByName(String name) {
        return departmentDao.getDepByName(name);
    }

    public DepartmentDao getDepartmentDao() {
        return departmentDao;
    }

    public void setDepartmentDao(DepartmentDao departmentDao) {
        this.departmentDao = departmentDao;
    }
}
